﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Betfairer
{
    public class Table
    {
        //private List<Cell[]> _table;
        private Cell[][] _table;
        public Decimal InPair { get; set; }
        //private int _hight, _width;

        public Table(List<Cell[]> table, decimal inPair)
        {
            _table = new Cell[table.Count][];

            for (int i = 0; i < table.Count; ++i)
            {
                _table[i] = table[i];
            }

            InPair = inPair;
        }

        //public Table(int hight, int width)
        //{
        //    _hight = hight;
        //    _width = width;
        //    _table = new Cell[_hight, _width];
        //}

        //public Table(BetfairUk.MarketPrices marketPrices)
        //{
        //    if (marketPrices == null)
        //    {
        //        Console.WriteLine("Table() error: bad odds.");
        //        _hight = 0;
        //        _width = 0;
        //        _table = new Cell[_hight, _width];
        //    }
        //    else
        //    {
        //        _hight = GetTableHight(marketPrices);
        //        _width = GetTableWidth(marketPrices);
        //        _table = new Cell[_hight, _width];

        //        FillTable(marketPrices);
        //    }
        //}

        //// should be better tested
        //public Table(BetfairUk.MarketPrices marketPrices, int hight, int width)
        //{
        //    if (marketPrices == null)
        //    {
        //        Console.WriteLine("Table() error: bad odds.");
        //        _hight = 0;
        //        _width = 0;
        //        _table = new Cell[_hight, _width];
        //    }
        //    else if (GetTableHight(marketPrices) > hight || GetTableWidth(marketPrices) > width)
        //    {
        //        Console.WriteLine("Table() error: high-width values are invalid.");
        //    }
        //    else
        //    {
        //        _hight = hight;
        //        _width = width;

        //        _table = new Cell[_hight, _width];

        //        FillTable(marketPrices);
        //    }
        //}

        //private void FillTable(BetfairUk.MarketPrices marketPrices)
        //{
        //    for (int i = 0; i < _hight; ++i)
        //    {
        //        BetfairUk.RunnerPrices row = marketPrices.runnerPrices[i];

        //        for (int j = 0; j < _width / 2; ++j)
        //        {
        //            _table[i, _width / 2 - j - 1] = j < row.bestPricesToBack.Length ?
        //                new Cell((decimal)row.bestPricesToBack[j].price, (decimal)row.bestPricesToBack[j].amountAvailable) : Cell.Empty;
        //            _table[i, _width / 2 + j] = j < row.bestPricesToLay.Length ?
        //                new Cell((decimal)row.bestPricesToLay[j].price, (decimal)row.bestPricesToLay[j].amountAvailable) : Cell.Empty;
        //        }
        //    }
        //}

        public int Hight
        {
            get
            {
                return _table.Length;
            }
        }

        public int Width
        {
            get
            {
                return _table[0].Length;
            }
        }

        //public void Set(Cell cell, int x, int y)
        //{
        //    _table[x, y] = cell;
        //}

        public Cell Get(int x, int y)
        {
            return _table[x][y];
        }

        //private int GetTableHight(BetfairUk.MarketPrices marketPrices)
        //{
        //    return marketPrices.runnerPrices.Length;
        //}

        //private int GetTableWidth(BetfairUk.MarketPrices marketPrices)
        //{
        //    int maximum = 0;

        //    for (int i = 0; i < marketPrices.runnerPrices.Length; ++i)
        //    {
        //        if (marketPrices.runnerPrices[i].bestPricesToBack.Length > maximum)
        //        {
        //            maximum = marketPrices.runnerPrices[i].bestPricesToBack.Length;
        //        }

        //        if (marketPrices.runnerPrices[i].bestPricesToLay.Length > maximum)
        //        {
        //            maximum = marketPrices.runnerPrices[i].bestPricesToLay.Length;
        //        }
        //    }

        //    return maximum * 2;
        //}

        public override string ToString()
        {
            string buffer = String.Empty;

            for (int i = 0; i < Hight; ++i)
            {
                for (int j = 0; j < Width; ++j)
                {
                    buffer += _table[i][j];
                }

                buffer += Environment.NewLine;
            }

            return buffer;
        }

        public String AsLine
        {
            get
            {
                string oddsBuffer = String.Empty;
                string moneyBuffer = String.Empty;

                for (int i = 0; i < Hight; ++i)
                {
                    for (int j = 0; j < Width; ++j)
                    {
                        oddsBuffer += String.Format("{0} ", _table[i][j].Coefficient);
                        moneyBuffer += String.Format("{0} ", _table[i][j].Amount);
                    }
                }

                // delete redundant last space
                moneyBuffer = moneyBuffer.Substring(0, moneyBuffer.Length - 1);

                return String.Concat(oddsBuffer, moneyBuffer);
            }
        }
    }
}
